package controller

import (
	"fmt"

	"time"

	"gitee.com/johng/gf/g"
	"gitee.com/johng/gf/g/encoding/gjson"
	"gitee.com/johng/gf/g/frame/gmvc"
	"gitee.com/johng/gf/g/net/ghttp"
	"gitee.com/johng/gf/g/os/gfile"
	"github.com/skratchdot/open-golang/open"
	"github.com/tealeg/xlsx"
)

type controllerHome struct {
	gmvc.Controller
}

type Response struct {
	Success int         `json:"success"`
	Msg     interface{} `json:"msg"`
}

var DB = g.DB()

func init() {
	ghttp.GetServer().BindController("/home", new(controllerHome))
}

/*
	获取参数(内部调用)
*/
func (h *controllerHome) getParams() map[string]string {
	return h.Request.GetRequestMap()
}

/*
	编辑页
*/
func (h *controllerHome) EditIndex() {
	h.View.Display("edit.html")
}

/*
	列表页
*/
func (h *controllerHome) Index() {
	h.View.Display("index.html")
}

/*
	增
*/
func (h *controllerHome) Appendata() {
	params := h.getParams()

	data := make(map[string]interface{})
	for k, v := range params {
		fmt.Printf("%s : %s\n", k, v)
		data[k] = v
	}

	res, err := DB.Table("qiyedb").Data(data).Insert()
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: err})
		return
	}

	if _, err := res.LastInsertId(); err == nil {
		h.Response.WriteJson(Response{Success: 0, Msg: "添加成功！"})
	} else {

		h.Response.WriteJson(Response{Success: 2, Msg: err})
	}
}

/*
	查
*/
func (h *controllerHome) Querydata() {

	res, err := DB.Table("qiyedb").All()
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: err})
		return
	}

	h.Response.WriteJson(res.ToList())

}

/*
	条件查询
*/
func (h *controllerHome) Selectdata() {
	params := h.getParams()
	dbmodel := DB.Table("qiyedb").Where(1)
	if params["shifouzhaopin"] != "无" {
		dbmodel = dbmodel.And("shifouzhaopin=?", params["shifouzhaopin"])
	}
	if params["riqi"] != "" {
		dbmodel.And("riqi=?", params["riqi"])
	}
	if params["qiyename"] != "" {
		dbmodel.And("qiyename like ?", "%"+params["qiyename"]+"%")
	}
	if params["biaoqian"] != "" {
		dbmodel.And("biaoqian like ?", "%"+params["biaoqian"]+"%")
	}
	if params["diqu"] != "" {
		dbmodel.And("diqu=?", params["diqu"])
	}
	res, err := dbmodel.Select()
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: err})
		return
	}

	h.Response.WriteJson(res.ToList())

}

/*
	删
*/
func (h *controllerHome) Removedata() {
	params := h.getParams()
	res, err := DB.Table("qiyedb").Where("id=?", params["id"]).Delete()
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: err})
	}
	if _, err := res.RowsAffected(); err == nil {
		h.Response.WriteJson(Response{Success: 0, Msg: "删除成功"})
	} else {
		fmt.Println(err)
		h.Response.WriteJson(Response{Success: 2, Msg: err})
	}
}

/*
	改
*/
func (h *controllerHome) Updatedata() {
	params := h.getParams()
	res, err := DB.Table("qiyedb").Data(params).Where("id=?", params["id"]).Update()
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: err})
	}
	if _, err := res.RowsAffected(); err == nil {
		h.Response.WriteJson(Response{Success: 0, Msg: "更改成功"})
	} else {
		fmt.Println(err)
		h.Response.WriteJson(Response{Success: 2, Msg: err})
	}
}

/*
	导出
*/
func (h *controllerHome) Exportdata() {
	params := h.Request.GetJson()
	excelFileName := "moban.xlsx"
	xlFile, err := xlsx.OpenFile(excelFileName)
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: "导出失败" + err.Error()})
		return
	}

	qiyes := gjson.New(params.ToMap()["data"]).ToArray()

	cells := xlFile.Sheets[0].Row(1).Cells
	for _, v := range qiyes {
		//获取第二行(表头)
		row := xlFile.Sheets[0].AddRow()
		for _, cv := range cells {
			cell := row.AddCell() //给个数组就好了
			for key, val := range gjson.New(v).ToMap() {
				if transform(key) == cv.String() {
					cell.Value = val.(string)
				}
			}
		}

	}
	filetime := time.Now().Format("2006年01月02日15时04分06秒")
	if !gfile.Exists(fmt.Sprintf("excels/%s", time.Now().Format("2006年01月02日"))) {
		gfile.Mkdir(fmt.Sprintf("excels/%s", time.Now().Format("2006年01月02日")))
	}
	filename := fmt.Sprintf("excels/%s/companyinfo%s导出.xlsx", time.Now().Format("2006年01月02日"), filetime)
	err = xlFile.Save(filename)
	if err != nil {
		h.Response.WriteJson(Response{Success: 2, Msg: "导出失败" + err.Error()})
		return
	}
	go func(filename string) {

		time.Sleep(3 * time.Second)
		open.Run(gfile.RealPath(filename))
	}(filename)
	h.Response.WriteJson(Response{Success: 0, Msg: "导出成功"})

}

/*
	下载
*/
func (h *controllerHome) Downloadtofile() {

}

func transform(key string) string {
	res := ""
	switch key {
	case "qiyename":
		res = "企业名称"

	case "laowu":
		res = "劳务"

	case "jingjirenticheng":
		res = "经纪人提成"

	case "huiyuanfanfei":
		res = "会员返费"

	case "zongfanfei":
		res = "总返费"

	case "fanfeitianshu":
		res = "返费天数"

	case "gongjia":
		res = "工价"

	case "xingbie":
		res = "性别"

	case "gongzhong":
		res = "工种"

	case "zhaopian":
		res = "照片"
	case "shifouzhaopin":
		res = "是否招聘"
	case "byzfyj":
		res = "毕业证复印件"

	case "sfzfyj":
		res = "身份证复印件"

	case "tijian":
		res = "体检"

	case "fanchangguiding":
		res = "返厂规定"

	case "yanba":
		res = "烟疤"

	case "wenshen":
		res = "纹身"

	case "xueli":
		res = "学历"

	case "shenfenzheng":
		res = "身份证"

	case "qita":
		res = "其他"

	case "banbie":
		res = "班别"

	case "guimo":
		res = "规模"

	case "diqu":
		res = "地区"

	case "zhanzuo":
		res = "站坐"

	case "zhuozhuang":
		res = "着装"

	case "gangwei":
		res = "岗位"

	case "changche":
		res = "厂车"

	case "zhusu":
		res = "住宿"

	case "huoshi":
		res = "伙食"

	case "xinzijiegou":
		res = "薪资结构"

	case "faxinri":
		res = "发薪日"

	case "zonghexinzi":
		res = "综合薪资"

	case "zhushengchan":
		res = "主生产"

	case "biaoqian":
		res = "标签"

	case "nianling":
		res = "年龄"

	case "id":
		res = "编号"
	case "dizhi":
		res = "地址"
	case "riqi":
		res = "日期"
	default:
		res = "未知字段" + key

	}

	return res
}
